Grammar Rules Builtin Predicate Set

Many Prolog implementations include a grammar rules facility that allows one to specify the grammar of a language using a form of context-free notation. When the Prolog reader reads a source file containing these rules it translates them into "pure Prolog" clauses. The grammar rules facility of PrologJ is based on that described in [Clocksin and Mellish 1994 ].

A grammar rule resembles an ordinary Prolog rule, except that --> is used instead of :-. In keeping with the requirements of a context free grammar, the left hand side is a single non-terminal symbol, and the right hand side is composed of a mixture of terminal and non-terminal symbols and can also include "pure Prolog" code.

The following is an example of a set of grammar rules. Notice that the non-terminal symbols can be augmented with one or more variables to receive information concerning the parse. Terminal symbols appear as lists of words. (In this example, each is one word; but a multi-element list representing a sequence of words is also allowed.) Pure Prolog code is delimited by braces.

sentence(S) --> 
	np(Subject), 
	verb(Verb), 
	np(Object),
	[.],
	{ S = sentence(subject(Subject), Verb, object(Object)) }.
	
np(NP) --> 
	determiner(D), 
	noun(N),
	{ NP = noun_phrase(D, N) }.
	
np(N) --> 
	noun(N).
	
determiner(article(indefinite)) --> [a].
determiner(article(definite)) --> [the].
noun(proper_noun(billy)) --> [billy].
noun(noun(ball)) --> [ball].
noun(noun(dog)) --> [dog].
verb(verb(hit)) --> [hit].
verb(verb(ran)) --> [ran].

The parsing of a sequence of words using the grammar rules is initiated by phrase/2.
 

phrase / 2
Prototype: phrase(Construct, Sentence)
Succeed if Sentence (a list) can be parsed as a Construct.
Example: phrase(sentence(S), [ billy, hit, the, dog, . ]) - would succeed if the above example grammar were loaded, and would instantiate S to sentence(subject(proper_noun(billy)), verb(hit), object(noun_phrase(article(definite), noun(dog))))